﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DiArte.Data.Catalog
{
    public class AttributeValueDal : BaseDal
    {
        /// <summary>
        /// Deletes all attribute values
        /// </summary>
        public void DeleteAll()
        {
            using (var command = new Npgsql.NpgsqlCommand("DELETE FROM attribute_value"))
                PerformCommand(command);
        }

        /// <summary>
        /// Inserts a new attribute value
        /// </summary>
        /// <param name="attributeId">The attribute id</param>
        /// <param name="code">The attribute value code</param>
        /// <param name="value">The value</param>
        /// <param name="isDefault">True if the value is the default for the attribute</param>
        /// <returns>The insert attribute value id</returns>
        public int Insert(int attributeId, String code, String value, Boolean isDefault)
        {
            using (var command = new Npgsql.NpgsqlCommand("INSERT INTO attribute_value(attribute_value_id, attribute_id, code, value, is_default) VALUES " +
                "(nextval('attribute_value_id_sequence'), :attribute_id, :code, :value, :is_default) RETURNING attribute_value_id"))
            {
                command.Parameters.AddWithValue("attribute_id", attributeId);
                command.Parameters.AddWithValue("code", code);
                command.Parameters.AddWithValue("value", value);
                command.Parameters.AddWithValue("is_default", isDefault);

                return PerformInsertCommandAndGetReturningValue(command);
            }
        }

    }
}
